<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\Models\Admin;
/**
 * Common 继承  判断没有登录的时候，跳转登录页面, 
 */
class LoginController extends CommonController{
    


	public function actionIndex(){
	   $model=new Admin();//模型

	   $model->setScenario('wang');//设置场景

	   if($model->load(Yii::$app->request->post())&&$model->validate()){
         //接值并判断规则

          if($model->check()){

            //随机加密通过路径加载而过;
          	$key=sha1('yjc'.date("Y-m-d").rand(000,999));
            
            $url="http://www.1705.com/advanced/backend/web/index.php?r=login/tiao&email=$model->email&key=$key";

            $mess="生活不是眼前的苟且";
            $good=self::SendMail($model->email,$url,$mess);
            // 判断邮件发送
            if($good){
            	echo "发送成功";
            }else{
            	echo "发送失败";
            }

          }else{
          	 // 无数据则返回
             Yii::$app->getSession()->setFlash('error',"邮箱不存在");
             return $this->render('index',['model'=>$model]);
            }
	   }else{
	         return $this->render('index',['model'=>$model]);
	       }	
	}

  // 测试邮箱
    static function SendMail($email,$mess){
        $model=new Admin();
        $mail= Yii::$app->mailer->compose(); 
        $mail->setTo($email); //要发送给那个人的邮箱 
        $mail->setSubject("邮件主题"); //邮件主题 
        $mail->setTextBody('测试text'); //发布纯文字文本 
        $mail->setHtmlBody($mess); //发送的消息内容 
        if($mail->send()){
            return true;
        }else{
            return false;
        }
    }

    public function actionTiao(){
          $model=new Admin();
          return $this->render('passwd',['model'=>$model]);
         // echo "验证码".sha1('yjc'.rand(000,999).date("Ymd"))."注意查收";
        }

}